<?php
namespace app\api\model;

use think\Model;


class Order extends Model
{

    // 表名
    protected $name = 'express_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];

    public function setOrderNoAttr($value,$data){
        return substr(time(),-8).substr(substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8).$value,-8);
    }

    public function getStatusList()
    {
        return ['created' => __('Status created'), 'package' => __('Status package')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function address()
    {
        return $this->belongsTo('OrderAddress', 'id', 'order_id', [], 'LEFT')->setEagerlyType(0);
    }

    public function goods(){
        return $this->hasMany('OrderGoods','order_id','id');
    }
}